﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;


namespace DAO
{
    public class ketnoi
    {
        private static string s = @"Data Source=HUNGLE-PC\SQLEXPRESS;Initial Catalog=QLDT;Integrated Security=True";
       private static SqlConnection connect;
        public ketnoi(string sql)
        {
            s = sql;
        }
        public ketnoi()
        {
            try
            {
                s = @"Data Source=HUNGLE-PC\SQLEXPRESS;Initial Catalog=QLDT;Integrated Security=True";
                connect = new SqlConnection(s);
                connect.Open();
            }
            catch
            { }
        }
       /* public ketnoi(string server, string name, string pass)
        {
            try
            {
                s = @"Server=" + server + ";UID=" + name + ";PWD=" + pass + ";Database=QLDT";
                connect = new SqlConnection(s);
                connect.Open();
            }
            catch
            { }
        }*/
        public static SqlConnection Cmb()
        {
            SqlConnection cn = new SqlConnection(s);
            return cn;
        }
        public bool Open()
        {
            try
            {
                connect = new SqlConnection(s);
                connect.Open();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public void Disconnect()
        {
           
            {
                connect.Close();
             
            }
        }
        public static DataTable ExcecuteQuery(string sql)
        {
            try
            {
                SqlConnection cn = new SqlConnection(s);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.CommandText = sql;
                SqlDataAdapter dt = new SqlDataAdapter(cmd);
                DataTable kq = new DataTable();
                dt.Fill(kq);
                cn.Close();
                return kq;
            }
            catch (Exception ex)
            {
                DataTable dt = new DataTable();
                return dt;
            }
        }
        public static DataTable ExcecuteQuery1(string sql)
        {
            try
            {
                SqlConnection cn = new SqlConnection(s);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = sql;
                SqlDataReader daReader = cmd.ExecuteReader();

                DataTable kq = new DataTable();
                kq.Load(daReader);
                cn.Close();
                return kq;
            }
            catch (Exception e)
            {
                DataTable dt = new DataTable();
                return dt;
            }
        }
        
        public static bool ExcuteNonQuery(string sql)
        {
            try
            {
                SqlConnection cn = new SqlConnection(s);
                cn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
                cn.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static DataTable selectAllImage()
        {
            SqlConnection cn = new SqlConnection(s);
            SqlDataAdapter dda = new SqlDataAdapter("getAllImage", cn);
            dda.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            if (cn.State == ConnectionState.Closed)
            {
                cn.Open();
            }
            dda.Fill(dt);
            cn.Close();
            return dt;
        }

        public static DataTable getImage(string manv)
        {
            SqlConnection cn = new SqlConnection(s);
            SqlDataAdapter dda = new SqlDataAdapter("getImg", cn);
            dda.SelectCommand.Parameters.AddWithValue("@manv", manv);
            dda.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            if (cn.State == ConnectionState.Closed)
            {
                cn.Open();
            }
            dda.Fill(dt);
            cn.Close();
            return dt;
        }
    }
}
